package kr.etcsoft.examples.binding.viewer.table;

import java.util.ArrayList;
import java.util.List;

import kr.etcsoft.examples.binding.model.BindableObject;

import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Observables;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

public class ColorLabelView extends ViewPart {
    private TableViewer viewer;

    public ColorLabelView() {

    }

    @Override
    public void createPartControl(Composite parent) {
        Table table = new Table(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("No");
        column.setWidth(20);

        column = new TableColumn(table, SWT.NONE);
        column.setText("Name");
        column.setWidth(100);

        viewer = new TableViewer(table);

        final List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("A", Person.FEMALE));
        persons.add(new Person("B", Person.MALE));
        persons.add(new Person("C", Person.FEMALE));
        persons.add(new Person("D", Person.MALE));

        ObservableListContentProvider contentProvider = new ObservableListContentProvider();
        viewer.setContentProvider(contentProvider);

        IObservableMap[] attributes = BeansObservables
                .observeMaps(contentProvider.getKnownElements(), Person.class, new String[] {
                        "name", "gender" });

        class ColorLabelProvider extends ObservableMapLabelProvider implements ITableColorProvider {
            Color male = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
            Color female = new Color(Display.getDefault(), 255, 192, 203);

            ColorLabelProvider(IObservableMap[] attributes) {
                super(attributes);
            }

            // to drive home the point that attributes does not have to
            // match
            // the columns
            // in the table, we change the column text as follows:
            @Override
            public String getColumnText(Object element, int index) {
                if (index == 0) {
                    return Integer.toString(persons.indexOf(element) + 1);
                }
                return ((Person) element).getName();
            }

            @Override
            public Color getBackground(Object element, int index) {
                return null;
            }

            @Override
            public Color getForeground(Object element, int index) {
                if (index == 0)
                    return null;
                Person person = (Person) element;
                return (person.getGender() == Person.MALE) ? male : female;
            }

            @Override
            public void dispose() {
                super.dispose();
                female.dispose();
            }
        }
        viewer.setLabelProvider(new ColorLabelProvider(attributes));

        IObservableList observableList = Observables.staticObservableList(persons);
        viewer.setInput(observableList);

        table.getColumn(0).pack();
    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }

    class Person extends BindableObject {
        public static final int MALE = 0;
        public static final int FEMALE = 1;

        private String name;
        private int gender;

        public Person(String name, int gender) {
            this.name = name;
            this.gender = gender;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            firePropertyChange("name", this.name, this.name = name);
        }

        public int getGender() {
            return gender;
        }

        public void setGender(int gender) {
            firePropertyChange("gender", this.gender, this.gender = gender);
        }
    }
}
